/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Login.java
 *
 * Created on Nov 5, 2011, 4:26:12 PM
 */
package mpinventory;

import java.sql.*;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Timer;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

/**
 *
 * @author msi
 */
public class Login extends javax.swing.JPanel {

    private Connection conn;
    private Dimension size;
    private UserQuery userQuery=new UserQuery(conn);
    public int width;
    public int height;
    public int screenWidth;
    public int screenHeight;
    private boolean success=false;
    public MainMenu mainMenu;
    private JFrame mainMenuFrame;
    /** Creates new form Login */
    public Login() {
        initComponents();
    }
    public Login(Connection conn, int screenWidth, int screenHeight) {
        this.conn=conn;
        this.screenWidth=screenWidth;
        this.screenHeight=screenHeight;
        initComponents();
        size=this.getPreferredSize();
        width=size.width;
        height=size.height+35;
        userQuery=new UserQuery(conn);
    }
    @Override
    public int getWidth(){
        return width;
    }
    @Override
    public int getHeight(){
        return height;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel2 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        btnLogin = new javax.swing.JButton();
        txtPassword = new javax.swing.JPasswordField();
        txtUsername = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();

        jLabel2.setFont(new java.awt.Font("Arial Rounded MT Bold", 1, 15));
        jLabel2.setText("Username:");
        jLabel2.setToolTipText("");

        jLabel1.setFont(new java.awt.Font("Arial Rounded MT Bold", 1, 30));
        jLabel1.setText("Log in");

        btnLogin.setFont(new java.awt.Font("Arial Rounded MT Bold", 1, 15)); // NOI18N
        btnLogin.setText("Log in");
        btnLogin.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                loginActionPerformed(evt);
            }
        });

        txtPassword.setFont(new java.awt.Font("Arial Rounded MT Bold", 0, 15));

        txtUsername.setFont(new java.awt.Font("Arial Rounded MT Bold", 0, 15));

        jLabel3.setFont(new java.awt.Font("Arial Rounded MT Bold", 1, 15));
        jLabel3.setText("Password:");
        jLabel3.setToolTipText("");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(144, 144, 144)
                .addComponent(jLabel1)
                .addContainerGap(156, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addGap(30, 30, 30)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel3)
                                .addGap(21, 21, 21))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel2)
                                .addGap(18, 18, 18)))
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txtPassword, javax.swing.GroupLayout.DEFAULT_SIZE, 229, Short.MAX_VALUE)
                            .addComponent(txtUsername, javax.swing.GroupLayout.PREFERRED_SIZE, 228, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(122, 122, 122)
                        .addComponent(btnLogin, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 123, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(35, 35, 35))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(txtUsername, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtPassword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addGap(18, 18, 18)
                .addComponent(btnLogin)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void loginActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_loginActionPerformed
        if(userQuery.loginSuccess(txtUsername.getText(),txtPassword.getText())){
            JOptionPane.showMessageDialog(null, "Successfully Logged in as " +  userQuery.user.getName());
            mainMenuFrame=new JFrame("Global Air Technology Inventory");
            mainMenu=new MainMenu(conn, userQuery.user.getName(), txtUsername.getText(),screenWidth, screenHeight);
            mainMenuFrame.add(mainMenu);
            mainMenuFrame.setSize(mainMenu.getWidth(), mainMenu.getHeight());
            mainMenuFrame.setLocation((screenWidth-mainMenu.getWidth())/2,(screenHeight-mainMenu.getHeight())/2);
            mainMenuFrame.setResizable(false);
            mainMenuFrame.setVisible(true);
            mainMenuFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            success=true;
            ActionListener actionListener=new ActionListener(){
                @Override
                public void actionPerformed(ActionEvent actionEvent){
                    try{
                        if(mainMenu.backToLogin)
                            mainMenuFrame.dispose();
                        try{
                            if(mainMenu.hide)
                                mainMenuFrame.setVisible(false);
                            else if(!mainMenu.hide && !mainMenu.backToLogin && !mainMenuFrame.isVisible())
                                mainMenuFrame.setVisible(true);
                        }
                        catch(Exception e){
                            e.printStackTrace();
                        }
                    }
                    catch(Exception e){
                        e.printStackTrace();
                    }
                }
            };
            Timer timer=new Timer(0, actionListener);
            timer.start();
        }
        else
            JOptionPane.showMessageDialog(null, "         Access Denied!");

    }//GEN-LAST:event_loginActionPerformed

    public boolean isSuccessful(){
        return success;
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnLogin;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPasswordField txtPassword;
    private javax.swing.JTextField txtUsername;
    // End of variables declaration//GEN-END:variables
}
